const { exec } = require('child_process');
const { promisify } = require('util');
const fs = require('fs').promises;
const path = require('path');

const execAsync = promisify(exec);

async function buildPopup() {
  console.log('Building popup...');
  try {
    // 切换到popup目录并运行构建
    const { stdout, stderr } = await execAsync('cd popup && pnpm build', {
      cwd: path.resolve(__dirname),
      shell: 'powershell.exe'
    });
    
    console.log('Build output:', stdout);
    if (stderr) {
      console.error('Build stderr:', stderr);
    }
    console.log('Popup built successfully!');
  } catch (error) {
    console.error('Build failed:', error.message);
  }
}

async function watchPopup() {
  console.log('Watching popup for changes...');
  // 这里可以实现监听文件变化并重新构建的逻辑
  // 为简化起见，我们只实现构建功能
  await buildPopup();
}

// 根据命令行参数决定执行哪个函数
const command = process.argv[2];

if (command === 'build') {
  buildPopup();
} else if (command === 'watch') {
  watchPopup();
} else {
  console.log('Usage: node dev.js [build|watch]');
}